package com.ofps.web.controller.flow;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ofps.flow.domain.Dto.FlowInfoInstDto;
import com.ofps.flow.domain.Dto.FlowReviewDto;
import com.ofps.flow.domain.EnterFlowNodeInst;
import com.ofps.forms.common.utils.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ofps.common.annotation.Log;
import com.ofps.common.core.controller.BaseController;
import com.ofps.common.core.domain.AjaxResult;
import com.ofps.common.enums.BusinessType;
import com.ofps.flow.domain.EnterFlowInfoInst;
import com.ofps.flow.service.IEnterFlowInfoInstService;
import com.ofps.common.utils.poi.ExcelUtil;
import com.ofps.common.core.page.TableDataInfo;

/**
 * 流程信息实例Controller
 * flow_info_inst：
 * ● id：流程实例的唯一标识
 * ● flow_id：该流程实例对应的流程id
 * ● name：该流程的名称
 * ● user_id：建立该流程实例的用户（一般是职员）
 * ● form_id：该流程实例关联的表单id
 * ● status：流程实例的状态 0进行中/1已结束
 * ● revise：该流程实例关联的表单是否允许被修改（该字段和审核状态有关，由审核人员决定）
 *
 * @author ofps
 * @date 2022-06-01
 */
@RestController
@RequestMapping("/flowInfoInst")
public class EnterFlowInfoInstController extends BaseController {
    @Autowired
    private IEnterFlowInfoInstService enterFlowInfoInstService;

    /**
     * 审批流程
     */
    @PostMapping("/flowReview")
    public R flowReview(@RequestBody FlowReviewDto flowReviewDto) {

        return enterFlowInfoInstService.review(flowReviewDto.getOp(), flowReviewDto.getFlowInfo(), flowReviewDto.getNodeInst());
    }

    /***
     * 根据题目类型和flow_instId查询题目答案
     *
     */
    @RequestMapping("/queryAnswer")
    public R queryAnswer(@RequestBody Map<String, Object> parms) {
        if (!parms.containsKey("questionType") && !parms.containsKey("flowInstId") || !parms.containsKey("questionId"))
            return R.error("参数错误");
        Integer questionType = (Integer) parms.get("questionType");
        String flowInstId = (String) parms.get("flowInstId");
        String questionId = (String) parms.get("questionId");
        return enterFlowInfoInstService.queryAnswer(questionType, questionId, flowInstId);
    }

    /***
     * 根据用户id查询实例
     */
    @RequestMapping("/queryByUserId")
    public R queryByUserId(@RequestBody FlowInfoInstDto params) {
        return enterFlowInfoInstService.queryByUserId(params);
    }

    /**
     * 查询流程实例列表
     */
    @PreAuthorize("@ss.hasPermi('flowInfoInst:list')")
    @GetMapping("/list")
    public TableDataInfo list(EnterFlowInfoInst enterFlowInfoInst) {
        startPage();
        List<EnterFlowInfoInst> list = enterFlowInfoInstService.selectEnterFlowInfoInstList(enterFlowInfoInst);
        return getDataTable(list);
    }

    /**
     * 导出流程实例列表
     */
    @PreAuthorize("@ss.hasPermi('flowInfoInst:export')")
    @Log(title = "流程实例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EnterFlowInfoInst enterFlowInfoInst) {
        List<EnterFlowInfoInst> list = enterFlowInfoInstService.selectEnterFlowInfoInstList(enterFlowInfoInst);
        ExcelUtil<EnterFlowInfoInst> util = new ExcelUtil<EnterFlowInfoInst>(EnterFlowInfoInst.class);
        util.exportExcel(response, list, "流程实例数据");
    }

    /**
     * 获取流程实例详细信息
     */
    @PreAuthorize("@ss.hasPermi('flowInfoInst:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(enterFlowInfoInstService.getById(id));
    }

    /**
     * 新增流程实例
     */
    @PreAuthorize("@ss.hasPermi('flowInfoInst:add')")
    @Log(title = "流程实例", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EnterFlowInfoInst enterFlowInfoInst) {
        return toAjax(enterFlowInfoInstService.save(enterFlowInfoInst));
    }

    /**
     * 修改流程实例
     */
    @RequestMapping("/edit")
    public R edit(@RequestBody EnterFlowInfoInst enterFlowInfoInst) {
        boolean flag = enterFlowInfoInstService.updateById(enterFlowInfoInst);
        return flag ? R.ok("更新成功") : R.error("更新失败");
    }

    /**
     * 删除流程实例
     */
    @PreAuthorize("@ss.hasPermi('flowInfoInst:remove')")
    @Log(title = "流程实例", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(enterFlowInfoInstService.removeByIds(Arrays.asList(ids)));
    }

}
